<?php

require_once('inc/database.php');
require_once('inc/user.php');

class Addon {

	/**
	 * The ID of this action
	 * @var int
	 */
	private $id;

	private $isValid;
	private $type;
	private $user;
	private $title;
	private $date;
	private $rating;
	private $basicDetails;

	/**
	 * Creates a new instance of Addon.
	 * @param int $id The ID of the addon that this instance should represent.
	 */
	public function __construct($id) {
		assert(is_int($id));
		$this->id = $id;
	}

	/**
	 * Retrives the ID of this addon.
	 * @return int This addon's ID
	 */
	public function getId() {
		return $this->id;
	}

	/**
	 * Builds a link to this addon.
	 * @return String A HTML link to this addon
	 */
	public function buildLink() {
		if (!$this->basicDetails) {
			$this->getBasicDetails();
		}

		if (!$this->isValid) {
			return '(invalid addon)';
		}

		$res  = '<a href="/addon/' . $this->id . '">' . htmlentities($this->title);
		$res .= '</a>';

		return $res;
	}

	/**
	 * Retrieves basic details about this addon.
	 */
	private function getBasicDetails() {
		$sql  = 'SELECT addon_type, user_id, addon_title, addon_date, ';
		$sql .= 'addon_rating FROM addons WHERE addon_id = ' . $this->id;
		$res  = mysql_query($sql);

		if (mysql_num_rows($res) == 1) {
			$row = mysql_fetch_assoc($res);
			$this->type = $row['addon_type'];
			$this->user = new User((int) $row['user_id']);
			$this->title = $row['addon_title'];
			$this->date = (int) $row['addon_date'];
			$this->rating = (int) $row['addon_rating'];
			$this->isValid = true;
		} else {
			$this->isValid = false;
		}

		$this->basicDetails = true;
	}
}

?>